package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.ticket.domain.Ticket;
import cn.wolfcode.luowowo.ticket.domain.TicketDetail;
import cn.wolfcode.luowowo.ticket.domain.TicketOrder;
import cn.wolfcode.luowowo.ticket.service.ITicketDetailService;
import cn.wolfcode.luowowo.ticket.service.ITicketOrderService;
import cn.wolfcode.luowowo.ticket.service.ITicketService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/ticket")
public class TicketController {

    @Reference
    private ITicketService ticketService;
    @Reference
    private ITicketDetailService ticketDetailService;
    @Reference
    private ITicketOrderService ticketOrderService;


    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("tickets",ticketService.listTop4());
        model.addAttribute("ticketss",ticketService.list());
        return "/ticket/index";
    }

    @RequestMapping("/detail")
    public String detail(Long tid,Model model){
        TicketDetail td = ticketDetailService.get(tid);
        Ticket t = ticketService.get(tid);
        model.addAttribute("ticketDetail",td);
        model.addAttribute("ticket",t);
        return "/ticket/detail";
    }

    @RequestMapping("/reloadSubAndProd")
    public String reloadSubAndProd(Long ajaxDestId, Model model){
        List<Ticket> tickets = ticketService.selectByDestId(ajaxDestId);
        model.addAttribute("tickets",tickets);
        return "/ticket/template";
    }

    @RequestMapping("/reloadProd")
    public String reloadProd(Model model, Long ajaxDestId, Long ajaxSubjectId){
        List<Ticket> tickets = ticketService.selectByDestIdAndSubjectId(ajaxDestId,ajaxSubjectId);
        model.addAttribute("tickets",tickets);
        return "/ticket/subtemplate";
    }

    //订单
    @RequestMapping("/addOrder")
    public String addOrder(Model model, Long tid, @UserParam UserInfo userInfo){
        if(userInfo == null){
            throw new LogicException("请先登录");
        }
        Ticket ticket = ticketService.get(tid);
        TicketDetail ticketDetail = ticketDetailService.get(tid);
        model.addAttribute("ticketDetail",ticketDetail);
        model.addAttribute("ticket",ticket);
        return "/ticket/addOrder";
    }

    //订单生成
    @RequestMapping("/order")
    @ResponseBody
    public Object order(TicketOrder ticketOrder,Long ticketId){
        ticketOrderService.saveOrUpdate(ticketOrder,ticketId);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/orderList")
    public String orderList(Model model){
        List<TicketOrder> ticketOrders = ticketOrderService.listAll();
        model.addAttribute("orders",ticketOrders);
        return "/ticket/ticketOrderTemplate";
    }
}
